package mail.proxy;

import mail.entity.ProxyEntity;
import mail.util.RegexSearcher;
import org.json.JSONObject;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Objects;

import static com.han.http.util.Print.printlnErr;
import static com.han.http.util.Print.printlnInfo;

/**
 * @author: Hanjiafeng
 * @date: Created in 11:19 2019/3/4
 * @desc:
 */
public class ProxyManager {

    private static HttpClient client;

    private static HttpRequest.Builder builder;

    static {
        client = HttpClient.newBuilder().build();
        builder = HttpRequest.newBuilder()
                .timeout(Duration.ofMinutes(1));
    }

    public static ProxyEntity getProxy() {
        ProxyEntity proxyEntity = getAllProxy();
        while (Objects.isNull(proxyEntity) || !proxyEntity.getUsername().isEmpty()) {
            //如果代理用户名不为空,归还代理,并重新获取
            if (!Objects.isNull(proxyEntity)) {
                returnProxy(proxyEntity, 1);
            }
            proxyEntity = getAllProxy();
        }
        return proxyEntity;
    }

    private static ProxyEntity getAllProxy() {

        HttpRequest request = builder
                .copy()
                .uri(URI.create("http://10.76.81.201:8093/proxy/acquireProxy?webSite=credit_qqmail&channels=Webpage&address="))
                .build();
        ProxyEntity proxyEntity;
        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            String body = response.body();
            JSONObject rawJson = new JSONObject(body);
            //代理获取状态
            int code = rawJson.getInt("code");
            if (code != 4097) {
                printlnErr("代理获取失败:" + code);
                return null;
            }
            //数据区
            JSONObject dataJson = rawJson.getJSONObject("data");
            String id = dataJson.getString("id");
            String ip = dataJson.getString("ip");
            int port = dataJson.getInt("port");
            String username = dataJson.getString("username");
            String password = dataJson.getString("password");
            proxyEntity = new ProxyEntity(id, ip, port, username, password);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
            proxyEntity = null;
        }

        return proxyEntity;
    }

    public static void returnProxy(ProxyEntity proxyEntity, int status) {

        HttpRequest request = builder
                .copy()
                .uri(URI.create("http://10.76.81.201:8093/proxy/revertProxy?id=" + proxyEntity.getId() + "&status=" + status + "&message="))
                .build();
        try {
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            String body = response.body();
            String code = RegexSearcher.matchFirst(body, "(?<=\"code\":)((?!,).)*(?=,)");
            printlnInfo("归还代理,id:" + proxyEntity.getId() + ",code:" + code);
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}
